<?php

/**
 * Front controller plugin that registers and provides access to the compiler helper
 *
 * @author     Mon Zafra <monzee@gmail.com>
 * @package    Yano
 * @copyright  (c)2009 Mon Zafra
 * @license    New BSD
 */
class Yano_Template extends Zend_Controller_Plugin_Abstract
{
    protected $_helper;
    protected $_defaultHelperClass = 'Yano_Template_Compiler';
    protected $_helperConfig;

    /**
     * Constructor
     * @param array $config Options to pass to compiler helper
     */
    public function __construct($config = array())
    {
        $this->_helperConfig = $config;
    }

    /**
     * Set compiler helper
     * @param mixed $helper
     * @return Yano_Template
     */
    public function setHelper($helper)
    {
        $this->_helper = $helper;
        return $this;
    }

    /**
     * Return/instantiate compiler helper
     * @return Yano_Template_Compiler
     */
    public function getHelper()
    {
        if (empty($this->_helper)) {
            $helper = $this->_defaultHelperClass;
            $this->setHelper(new $helper($this->_helperConfig));
        }
        return $this->_helper;
    }

    /**
     * Register helper
     * @param Zend_Controller_Request_Abstract $request
     */
    public function preDispatch(Zend_Controller_Request_Abstract $request)
    {
        $helper = $this->getHelper();
        Zend_Controller_Action_HelperBroker::addHelper($helper);
    }

}
